package com.example.controller;

import com.example.controller.dto.AddItemRequest;
import com.example.controller.dto.UpdateItemRequest;
import com.example.model.Cart;
import com.example.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/api/cart")
public class CartController {

    private final CartService cartService;

    @Autowired
    public CartController(CartService cartService) {
        this.cartService = cartService;
    }

    private String getUserId(Authentication authentication) {
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        return userDetails.getUsername();
    }

    @GetMapping
    public ResponseEntity<Cart> getCart(Authentication authentication) {
        try {
            String userId = getUserId(authentication);
            Cart cart = cartService.getCart(userId);
            if (cart == null) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(cart);
        } catch (IOException e) {
            return ResponseEntity.status(500).build();
        }
    }

    @PostMapping("/items")
    public ResponseEntity<Void> addItemToCart(Authentication authentication, @RequestBody AddItemRequest request) {
        try {
            String userId = getUserId(authentication);
            cartService.addItemToCart(userId, request.getProductId(), request.getQuantity());
            return ResponseEntity.ok().build();
        } catch (IOException | IllegalArgumentException e) {
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/items/{productId}")
    public ResponseEntity<Void> updateCartItem(Authentication authentication, @PathVariable String productId, @RequestBody UpdateItemRequest request) {
        try {
            String userId = getUserId(authentication);
            cartService.updateCartItem(userId, productId, request.getQuantity());
            return ResponseEntity.ok().build();
        } catch (IOException e) {
            return ResponseEntity.status(500).build();
        }
    }

    @DeleteMapping("/items/{productId}")
    public ResponseEntity<Void> removeItemFromCart(Authentication authentication, @PathVariable String productId) {
        try {
            String userId = getUserId(authentication);
            cartService.removeItemFromCart(userId, productId);
            return ResponseEntity.ok().build();
        } catch (IOException e) {
            return ResponseEntity.status(500).build();
        }
    }

    @DeleteMapping
    public ResponseEntity<Void> clearCart(Authentication authentication) {
        try {
            String userId = getUserId(authentication);
            cartService.clearCart(userId);
            return ResponseEntity.ok().build();
        } catch (IOException e) {
            return ResponseEntity.status(500).build();
        }
    }
} 